package com.cloudeasy.area.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 区域路径工具类
 */
public class AreaPathUtil {
    /**
     * 获取父区域名称
     *
     * @param path 路径
     * @return {@link String}
     */
    public static String parseParent(String path) {
        if (path == null) {
            return null;
        }
        String s = StringUtils.strip(path, ",");
        String[] strings = s.split(",");
        if (strings.length < 2) {
            return null;
        }
        return strings[strings.length - 2];
    }

    /**
     * 判断是否包含另一个列表中的所有区域
     *
     * @param parentPaths 父区域路径列表
     * @param childPaths  子区域路径列表
     * @return {@link Boolean}
     */
    public static Boolean contains(List<String> parentPaths, List<String> childPaths) {
        parentPaths.forEach(parentPath -> {
            childPaths.removeIf(childPath -> StringUtils.contains(childPath, parentPath));
        });
        return childPaths.size() == 0;
    }
}
